import xlrd
import sys


class driver:
    def __init__(self):
        self.book = xlrd.open_workbook(r'..\DATA\TinyShop.xlsx')

    def readBook(self):
        sheet = self.book.sheets()[2]
        n = 1
        for i in range(1, sheet.nrows):
            list = sheet.row_values(i)
            need = list[7].split('\n')
            num = need[0][4:]
            goodsid = need[1][8:]
            addressid = need[2][10:]
            pamentid = need[3][10:]
            invicetype = need[4][11:]
            print('第{}次创建订单---购买数量:{} 商品id:{} 地址id:{} 支付方式id:{} 打字票据id: {} 期望结果: {}'.format(n, num, goodsid, addressid,
                                                                                            pamentid, invicetype,
                                                                                            list[9]))
            __import__('TinyShop17.CASE.' + list[0])
            m = sys.modules['TinyShop17.CASE.' + list[0]]
            type = getattr(m, list[1])
            method = getattr(type(num, goodsid, addressid, pamentid, invicetype), list[2])
            result = method()
            if list[9] in result:
                self.answer = '测试成功'
            else:
                self.answer = '测试失败'
            with open(r'..\DATA\TinyShop接口测试结果.txt', 'a+', encoding='utf-8') as f:
                f.write(list[0] + ',' + list[1] + ',' + list[2] + ',' + list[3] + ',' + list[6] + ',' + list[
                    9] + ',' + result + ',' + self.answer + '\n')
            print('实际结果: %s    测试结果:%s' % (result, self.answer))
            n += 1


if __name__ == "__main__":
    driver().readBook()
